package com.zhengsr.drawbroad.state;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import com.zhengsr.drawbroad.data.BaseData;
import com.zhengsr.drawbroad.utils.CommandUtils;
import com.zhengsr.drawbroad.utils.ConstractUtils;

/**
 * Created by zhengshaorui on 2017/12/23.
 * csdn: http://blog.csdn.net/u011418943
 */

public class PenState extends BaseState {


    private Path mPath;
    private Paint mPaint;
    private float mLastX,mLastY;
    private Canvas mCanvas;

    public PenState() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(10);
    }

    @Override
    public void onDraw(Canvas canvas) {
        mCanvas = canvas;
    }

    @Override
    public int getMode() {
        return ConstractUtils.PENCIL;
    }

    @Override
    public void drawDown(float x, float y) {
        if (mPath == null){
            mPath = new Path();
        }

        mLastX = x;
        mLastY = y;
        mPath.moveTo(x,y);
    }

    @Override
    public void drawMove(float x, float y) {
        //贝塞尔曲线画线
        mPath.quadTo(mLastX,mLastY,(x+mLastX)/2,(y+mLastY)/2);

        mCanvas.drawPath(mPath, mPaint);

        mLastX = x;
        mLastY = y;
    }

    @Override
    public void drawUp() {
        Path cachepath = new Path(mPath);
        Paint paint = new Paint(mPaint);
        BaseData data = new BaseData(cachepath,paint);
        CommandUtils.getInstance().add(data);
        mPath.reset();
    }

    @Override
    public void setPaintColor(int paintColor) {
        super.setPaintColor(paintColor);
        mPaint.setColor(paintColor);
    }
}
